﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FCISPlus.Client.Communicator;

namespace FCISPlus.Client.UI
{
    public partial class ManageFriends : Form
    {
        private IFCISPlusClientCommunicator communicator;
        FCISPlus.Server.Database.User mySelf;

        public ManageFriends(IFCISPlusClientCommunicator communicator)
        {
            InitializeComponent();
            this.communicator = communicator;
        }

        class listBoxItem
        {
            public string Username {get; set;}
            public bool isFriend {get; set;}
            public FCISPlus.Server.Database.User userData {get; set;}
        }
       
        private void ManageFriends_Load(object sender, EventArgs e)
        {
             mySelf = communicator.GetUserProfile();            
            this.Text =  mySelf.Username + " Friends";

            ReloadFriendsData();
        }

        private void ReloadFriendsData()
        {
            lstAll.DataSource = null;
            lstFriends.DataSource = null;
            lstFriends.Items.Clear();
            lstAll.Items.Clear();

            var allUsers = communicator.GetAllUsers();
            var friends = communicator.GetFriends();            

            lstAll.DisplayMember = "Username";
            lstAll.DataSource = (from user in allUsers
                                 where user.UserID != mySelf.UserID
                                 select new listBoxItem()
                                 {
                                     Username = user.Username + (friends.Contains(user) == true ? " (Friend)" : "")
                                     ,isFriend = friends.Contains(user)
                                     ,userData = user
                                 }).ToList();

            lstFriends.DisplayMember = "Username";
            lstFriends.DataSource = friends.ToList();
        }        

        private void btnAddFriend_Click(object sender, EventArgs e)
        {
            if (lstAll.SelectedItem != null)
            {
                listBoxItem item = (listBoxItem)(lstAll.SelectedItem);
                communicator.AddFriend(item.userData);                
                ReloadFriendsData();
            }
        }

        private void btnRemoveFriend_Click(object sender, EventArgs e)
        {
            if (lstFriends.SelectedItem != null)
            {
                communicator.RemoveFriend((FCISPlus.Server.Database.User)lstFriends.SelectedItem);
                ReloadFriendsData();
            }
        }

        private void lstAll_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstAll.SelectedItem != null)
            {
                listBoxItem item = (listBoxItem)(lstAll.SelectedItem);
                if (item.isFriend == true) 
                    btnAddFriend.Enabled = false;
                else
                    btnAddFriend.Enabled = true;
            }
        }

 
    }
}
